// pages/fan/index.js
const app = getApp()
import * as echarts from '../..//components/ec-canvas/echarts';
let _timer1 = null;
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    title: {},
    legend: {
      data: ['昨天', '今天'],
      top: 10,
      left: 'center',
      backgroundColor: 'white',
      z: 100
    },
    grid: {
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: '昨天',
      type: 'line',
      smooth: true,
      data: [18, 36, 65, 30, 78, 40, 33, 36, 65, 30, 78, 40, 33, 36, 65, 30, 78, 40, 33, 36, 65, 30, 78, 40]
    }, {
      name: '今天',
      type: 'line',
      smooth: true,
      data: [12, 50, 51, 35, 70, 3, 12, 50, 51, 35, 70, 30, 12, 50, 51, 35, 70, 30, 12, 50, 51, 35, 70, 30]
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: null,
    deviceId: null,
    frequency: 0,
    ec: {
      onInit: initChart
    },
    clicking: false,
    isRunning: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let deviceId = options.deviceId
    if (deviceId != null) {
      this.setData({
        deviceId: deviceId
      })
      app.api.getDeviceDetail(deviceId).then(res => {
        if (res.data != null) {
          if (res.data.workMode != '0') {
            res.data.workMode = '1'
          }
          if(res.data.power != null){
            res.data.isRunning = res.data.power
          }
          this.setData({
            device: res.data,
            frequency: 60 * (res.data.frequency != null ? parseInt(res.data.frequency) : 0),
            isRunning: res.data.isRunning
          })
          wx.setNavigationBarTitle({
            title: res.data.deviceName
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    let _this = this
    _timer1 = setInterval(function () {
      if (_this.data.deviceId != null) {
        app.api.getDeviceDetail(_this.data.deviceId).then(res => {
          if (res.data != null) {
            if (res.data.workMode != '0') {
              res.data.workMode = '1'
            }
            if(res.data.power != null){
              res.data.isRunning = res.data.power
            }
            _this.setData({
              device: res.data,
              frequency: 60 * (res.data.frequency != null ? parseInt(res.data.frequency) : 0),
              isRunning: res.data.isRunning
            })
          }
        })
      }
    }, 2500)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  changeStatus() {
    let _this = this;
    let content = ''
    if (_this.data.device.isRunning == '1') {
      content = '确认是否关机？'
    } else {
      content = '确认是否启动？'
    }
    wx.showModal({
      title: '提示',
      content: content,
      complete: (res) => {
        if (res.cancel) {

        }
        if (res.confirm) {
          let device = _this.data.device;
          if (device.isRunning == '1') {
            device.isRunning = '0'
          } else {
            device.isRunning = '1'
          }
          device.commandKey = 'power'
          device.commandValue = device.isRunning
          app.api.updateDevice(device).then(res => {
            if (res.code == 200) {
              _this.setData({
                device: device,
                isRunning: device.isRunning
              })
            }
          })
        }
      }
    })
  },
  changeMode() {
    if (this.data.clicking == true) {
      return;
    }
    this.setData({
      clicking: true
    })
    let device = this.data.device;
    if (device.workingMode == '1') {
      device.workingMode = '0'
    } else {
      device.workingMode = '1'
    }
    device.commandKey = 'mode'
    device.commandValue = device.workingMode
    app.api.updateDevice(device).then(res => {
      if (res.code == 200) {
        this.setData({
          device: device
        })
      }
    })
    let _this = this;
    setTimeout(function () {
      _this.setData({
        clicking: false
      })
    }, 1500)
  },
})